package com.itlxd.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itlxd.entity.Employee;
import com.itlxd.enums.EYesOrNo;
import com.itlxd.exception.ArgsException;
import com.itlxd.exception.LoginException;
import com.itlxd.global.ErrorCode;
import com.itlxd.global.HttpCode;
import com.itlxd.global.SysConstants;
import com.itlxd.global.SysMessages;
import com.itlxd.result.ResultVO;
import com.itlxd.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 员工登陆
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public ResultVO login(@RequestBody Employee employee, HttpSession session) {
        if (null == employee) {
            return ResultVO.error(SysMessages.ARGS_IS_NULL);
        }
        if (StringUtils.isEmpty(employee.getUsername()) || StringUtils.isEmpty(employee.getPassword())) {
            return ResultVO.error(HttpCode.ERROR, SysMessages.USERNAME_PASSWORD_IS_NULL);
        }

        Employee loginEmployee = employeeService.getOne(employee);
        if (loginEmployee == null) {
            return ResultVO.error(HttpCode.ERROR, SysMessages.USERNAME_PASSWORD_ERROR);
        }

        if (loginEmployee.getStatus() == EYesOrNo.NO) {
            return ResultVO.error(HttpCode.ERROR, SysMessages.EMPLOYEE_IS_FORBIDDEN);
        }

        session.setAttribute(SysMessages.LOGIN_EMPLOYEE, loginEmployee);
        return ResultVO.success(loginEmployee, SysMessages.LOGIN_SUCCESS);
    }

    /**
     * 退出登陆
     *
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public ResultVO<String> logout(HttpSession session) {
        session.removeAttribute(SysConstants.LOGIN_EMPLOYEE);
        return ResultVO.success(SysMessages.LOGOUT_SUCCESS);
    }

    /**
     * 添加员工
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public ResultVO<Integer> addEmployee(@RequestBody Employee employee, HttpSession session) {
        //参数校验
        if (null == employee) {
            throw new ArgsException(SysMessages.ARGS_EXCEPTION);
        }
        if (StringUtils.isEmpty(employee.getUsername()) || StringUtils.isEmpty(employee.getName())
                || StringUtils.isEmpty(employee.getPhone()) || StringUtils.isEmpty(employee.getSex())
                || StringUtils.isEmpty(employee.getIdNumber())) {
            throw new ArgsException(SysMessages.ARGS_EXCEPTION);
        }
        //检验用户名长度
        if (employee.getUsername().length() > 20 || employee.getUsername().length() < 3) {
            throw new ArgsException(ErrorCode.INSERT_DEFAULT_ERROR, SysMessages.USERNAME_LENGTH_ERROR);
        }

        //判断用户名是否存在
        Boolean isExist = employeeService.usernameIsExist(employee.getUsername());
        if (isExist) {
            throw new ArgsException(SysMessages.USERNAME_IS_EXIST);
        }

        Integer addNum = employeeService.saveEmployee(employee);
        //判断是否操作成功
        if (addNum > SysConstants.NUM_ZERO) {
            return ResultVO.success(addNum, SysMessages.INSERT_SUCCESS);
        }
        return ResultVO.error(ErrorCode.INSERT_DEFAULT_ERROR, SysMessages.INSERT_FAIL);
    }


    /**
     * 分页查询员工列表
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public ResultVO page(Integer page, Integer pageSize, String name) {
        if (null == page || null == pageSize) {
            throw new ArgsException(SysMessages.ARGS_IS_NULL);
        }

        IPage<Employee> employeeIPage = employeeService.listPageEmployees(page, pageSize, name);
        ResultVO<IPage<Employee>> success = ResultVO.success(employeeIPage, SysMessages.QUERY_SUCCESS);
        return success;
    }

    /**
     * 修改用户状态
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/{id}/{status}")
    public ResultVO<Integer> switchStatus(@PathVariable(name = "id") Long id, @PathVariable(name = "status") Integer status) {
        if (null == id || status == null) {
            throw new ArgsException(SysConstants.ARGS_IS_NULL, SysMessages.ARGS_IS_NULL);
        }

        Employee employee = new Employee();
        employee.setId(id).setStatus(status);

        Integer effectNum = employeeService.updateEmployee(employee);

        if (effectNum > SysConstants.NUM_ZERO) {
            return ResultVO.success(effectNum, SysMessages.UPDATE_SUCCESS);
        }
        return ResultVO.error(ErrorCode.INSERT_DEFAULT_ERROR, SysMessages.UPDATE_FAIL);
    }

    /**
     * 通过id查询员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultVO<Employee> queryEmployeeById(@PathVariable Long id) {
        if (null == id) {
            throw new ArgsException(SysMessages.ARGS_IS_NULL);
        }

        Employee employee = employeeService.getById(id);
        if (null == employee) {
            return ResultVO.error(ErrorCode.QUERY_DEFAULT_ERROR, SysMessages.EMPLOYEE_IS_NOT_EXIST);
        }

        return ResultVO.success(employee, SysMessages.QUERY_SUCCESS);
    }

    /**
     * 更新员工信息
     *
     * @param employee
     * @return
     */
    @PutMapping
    public ResultVO<Integer> updateEmployee(@RequestBody Employee employee) {
        if (null == employee || null == employee.getId()) {
            throw new ArgsException(SysMessages.ARGS_IS_NULL);
        }

        Integer effectNum = employeeService.updateEmployee(employee);
        if (null != effectNum && effectNum > SysConstants.NUM_ZERO) {
            return ResultVO.success(effectNum, SysMessages.UPDATE_SUCCESS);
        }
        return ResultVO.error(ErrorCode.UPDATE_DEFAULT_ERROR, SysMessages.UPDATE_FAIL);
    }

}

